var fy_api = {
    split: "⊙",
    /*访问根路径配置*/
    server_path: "http://localhost:8081/logistics-web-server/",//<开发环境>
    // server_path: "http://xcxtest.feiyang56.cn:40261/xcxtest-web-server/",//<测试环境>
    // server_path: "http://logistics.feiyang56.cn:40215/logistics-web-server/",//<正式环境>
    ajax_defaultOption: {
        type: "POST",
        urlAppend: true,//默认追加服务器地址
        loginVal: true,//默认验证登录状态
        error: function (e) {
            if (typeof(hideLoading) == "function")
                hideLoading();
            if (e.statusText == "abort") {
                return;
            }
            if (e && typeof(e.responseText) == "string") {
                try {
                    var response = eval("[" + e.responseText + "]")[0];
                    if (response.code == 601) {
                        $.messager.alert('系统提示', '操作失败,您无此权限!', 'error');
                    } else if (response.code == 600) {
                        $.messager.alert('系统提示', '操作失败,登录超时或者服务器异常!', 'error', function () {
                            window.top.location.href = fy_api.login_path;
                        });
                    }
                } catch (e) {
                    $.messager.alert('系统提示', '操作失败,请稍后再试或者联系管理员!', 'error');
                }
            } else {
                $.messager.alert('系统提示', '操作失败,请稍后再试或者联系管理员!', 'error');
            }
        }
    },
    load_template: function (option) {
        option.controller.each(function () {
            var t = $(this);
            t.after('<script type="text/html">' + t.html() + '</script>');
            if (typeof(option.data) != "undefined") {
                //本地加载
                fy_api.load(t, option.data);
                if (typeof(option.loaded) == "function") {
                    option.loaded.call(t);
                }
            } else {
                //远程加载
                var param = t.attr("templateParam");
                if (typeof(param) != "undefined") {
                    var p = param.split("&");
                    param = {};
                    for (var i = 0; i < p.length; i++) {
                        var _p = p[i].split("=");
                        param[_p[0]] = _p[1];
                    }
                }
                if (option.loading) showLoading();
                fy_api.ajax({
                    data: param,
                    url: t.attr("templateUrl"),
                    success: function (data) {
                        if (option.loading) hideLoading();
                        var param = {};
                        if (typeof(t.attr("templateRoot")) != "undefined") {
                            param[t.attr("templateRoot")] = data;
                        } else
                            param = data;
                        fy_api.load(t, param);
                        if (option.initValue) {
                            t.find("select[value]").each(function () {
                                var value = $(this).attr("value");
                                if (value != "") {
                                    $(this).val(value);
                                }
                            })
                        }
                        if (typeof(option.loaded) == "function") {
                            option.loaded.call(t);
                        }
                    }
                })
            }
        })
    },
    load: function (jqueryObj, data) {
        if (jqueryObj.length > 0) {
            var nextObj = jqueryObj.next();
            if (nextObj.length > 0) {
                jqueryObj.html(template.compile(nextObj.html())(data));
                jqueryObj.find("script[type='text/html']").each(function(){
                    $(this).after($(this).html()).remove();
                })
                if(easyuiload){
                    //解决阿里控件form里不解析控件的问题20171114
                    //需要加载easyui
                    $.parser.parse(jqueryObj);
                }
                nextObj.remove();
            }
        }
    },
    ajax: function (ajax_option) {
        ajax_option = $.extend({}, fy_api.ajax_defaultOption, ajax_option);
        if (ajax_option.urlAppend) ajax_option.url = fy_api.server_controller_path + ajax_option.url;
        if (ajax_option.loginVal || ajax_option.loading) {
            var success = ajax_option.success;
            if (ajax_option.loading) showLoading();
            ajax_option.success = function () {
                if (ajax_option.loading) hideLoading();
                if (arguments.length > 0) {
                    if(fy_api.isBadRequest(arguments[0])){
                        hideLoading();
                        $.messager.alert('系统提示', '操作失败,登录超时或者服务器异常!', 'error', function () {
                            window.top.location.href = fy_api.server_path + 'index.html?msg=reqerr';
                        });
                        return;
                    }
                    if (!ajax_option.loginVal || fy_api.success(arguments[0], arguments[1], arguments[2]) && arguments.length == 3)
                        success.call(this, arguments[0], arguments[1], arguments[2]);
                }
            };
        }
        return $.ajax(ajax_option);
    }, ajaxSubmit: function (jqueryObj, ajax_option) {
        ajax_option = $.extend({}, fy_api.ajax_defaultOption, ajax_option);
        ajax_option.type = undefined;
        if (ajax_option.urlAppend) ajax_option.url = fy_api.server_controller_path + jqueryObj.attr("action");
        if (ajax_option.loginVal || ajax_option.loading) {
            var success = ajax_option.success;
            if (ajax_option.loading) showLoading();
            ajax_option.success = function () {
                if (ajax_option.loading) hideLoading();
                if (arguments.length > 0) {
                    if(fy_api.isBadRequest(arguments[0])){
                        hideLoading();
                        $.messager.alert('系统提示', '操作失败,登录超时或者服务器异常!', 'error', function () {
                            window.top.location.href = fy_api.server_path + 'index.html?msg=reqerr';
                        });
                        return;
                    }
                    if (!ajax_option.loginVal || fy_api.success(arguments[0], arguments[1], arguments[2]) && arguments.length >= 3)
                        success.call(jqueryObj[0], arguments[0], arguments[1], arguments[2], arguments[3]);
                }
            };
        }
        return jqueryObj.ajaxSubmit(ajax_option);
    }, success: function () {
        if (arguments.length > 0) {
            var r = arguments[0];
            if (typeof(r) == "string" && r.substr(0, 1) == "{") {
                if (typeof(hideLoading) == "function")
                    hideLoading();
                try {
                    var response = eval("[" + r + "]")[0];
                    if (response.code == 600) {
                        $.messager.alert('系统提示', '操作失败,登录超时或者服务器异常!', 'error', function () {
                            window.top.location.href = fy_api.login_path;
                        });
                    }
                    return false;
                } catch (e) {
                }
            }
        }
        return true;
    },
    getLoginUser: function (callBack) {
        //获取当前登录用户
        fy_api.ajax({
            url: "user/getLoginUser.do",
            success: function (user) {
                if (typeof(callBack) == "function") {
                    callBack(user);
                }
            },
            error: function (msg) {
                console.log("error:"+msg);
            }
        })
    }
    ,
    //验证ajax提交后返回结果是否为过滤器返回结果
    isBadRequest:function(res){
        // 判断是否是json
        if (typeof res == 'string' && res.indexOf("<script type=\"text/javascript\">") != -1 && res.indexOf("/index.html?msg=reqerr'</script>") != -1) {
            return true;
        }
        return false;
    }
};
fy_api.server_controller_path = fy_api.server_path + "controller/";
fy_api.login_path = "http://192.168.3.222:8081/cas-server/login?service=" + fy_api.server_path;